﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sora.HelpSystem.Package;
using System.Collections.ObjectModel;
using System.Windows.Data;
using Sora.HelpSystem.Package.Pages;

namespace Sora.HelpSystem.Editor.Logic
{
    public class HelpDocumentEditorRoot
    {
        public HelpDocument Document { get; private set; }

        public HelpDocumentProvider DocumentProvider { get; private set; }

        public HelpPage HelpRoot { get; private set; }

        private Uri Combine(Uri a, string b)
        {
            return new Uri(a.AbsoluteUri + "/" + b);
        }

        private bool IsNullUri(Uri a)
        {
            return a == null || String.IsNullOrEmpty(a.AbsoluteUri);
        }

        public HelpDocumentEditorRoot(HelpDocument document)
        {
            if (document == null)
                throw new ArgumentNullException("document");

            Document = document;
            DocumentProvider = new HelpDocumentProvider(Document);

            if(IsNullUri(Document.DocumentBaseUri))
                Document.DocumentBaseUri = new Uri("ds-help://help.document");

            /* on s'assure que la page de démarrage existe */
            if(IsNullUri(Document.StartPageUri))
                Document.StartPageUri = Combine(Document.DocumentBaseUri, "start");

            DocumentProvider.ResolveUri(Document.StartPageUri, true);

            /* on charge la racine */
            HelpRoot = DocumentProvider.ResolveUri(Document.DocumentBaseUri, true);
        }
    }
}
